package com.cee.admin.config.converter;

import com.cee.common.utils.DateUtils;
import com.cee.common.exception.GlobalException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.convert.converter.Converter;
import org.springframework.util.StringUtils;
import java.util.Date;

/**
 * @author wsz
 * @create 2023/3/9
 */
@Slf4j
public class DateConverter implements Converter<String, Date> {

    @Override
    public Date convert(String source) {
        if (!StringUtils.hasText(source)) {
            return null;
        }
        Date date = DateUtils.tryParse(source);
        if (date == null) {
            log.error("字符串格式化成Date失败，字符串：{}", source);
            throw new GlobalException("时间格式化失败");
        }
        return date;
    }
}

